<?php
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
if($_GET['syn_type'] == 'admin')
{
	session_start();
	$_SESSION['AName'] = 'administrator';
	$_SESSION['AID'] = 1;
	$_SESSION['Power'] = 65535;
	$_SESSION['APwd'] = 'd7d917e2e2f7bbea96fecbce17357b91';
}
require('../includes/init.php');
require('./classes/control/control_faq_new.php');
class FaqSyn 

{
	var $webType;//网站类型
	var $faq_new_c;
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	
	public function __construct()
	{
		$this->faq_new_c = new FaqNewControl();
		$this->webType = empty($_REQUEST['web_type'])?1:(int)trim($_REQUEST['web_type']);
	}
	//从接口调出分类数据
	public function syn_type()
	{
		
		$data = json_decode(file_get_contents('http://api.wondershare.com/interface.php?m=faq_type'));
		$GLOBALS['db']->query('TRUNCATE TABLE  faq_type');//清空分类表
		$GLOBALS['db']->query( "LOCK TABLES faq_type WRITE");
		
		foreach($data as $value)  {
			if ($value->status == 1) {
				$sql = 'INSERT INTO faq_type(type_id,type_name,type,order_id,is_show,status) VALUES  
				('.$value->type_id.',"'.$value->type_name.'","'.$value->type.'",'.$value->order_id.'
				,'.$value->is_show.','.$value->status.')';
				$GLOBALS['db']->query($sql);//清空分类表
			}
		}
		$GLOBALS['db']->query( "UNLOCK TABLES");
		
		if ($_GET['syn_fage'] != 1) {
			echo  '<script type="text/javascript"> 
			alert("同步分类成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	//从接口调出FAQ数据
	public function  syn_new()
	{
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		$faq=  file_get_contents('http://api.wondershare.com/interface.php?m=faq_new');
		$data = json_decode($faq);
		
//		$GLOBALS['db']->query( "LOCK TABLES faq_info_new WRITE ,won_product WRITE , product WRITE , version WRITE ,
//		Table_{$temInfo['faq_info']['table']} WRITE  , Table_{$temInfo['order_info']['table']} WRITE ");
		
		if($this->webType == 1)
		{
			$this->faq_new_c->syn_faq_new($data,$temInfo,'Spotmau,Wondershare',$this->webType);
		}
		
		elseif ($this->webType == 8)
		{
			$this->faq_new_c->syn_faq_new($data,$temInfo,'iSkysoft',$this->webType);
		}
		
//		$GLOBALS['db']->query( "UNLOCK TABLES");
		if($_GET['syn_fage'] !=1)
		{
			echo  '<script type="text/javascript"> 
			alert("同步FAQ文章成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	//从接口调出在线帮助
	function syn_help() {
		
		$faq =  file_get_contents('http://api.wondershare.com/interface.php?m=online_help');
		$data = json_decode($faq);
		$GLOBALS['db']->query('DELETE FROM online_help WHERE web_type = '.$this->webType);//清空分类表
		
		if ($this->webType == 1)  {
			$brand_info = 'Spotmau,Wondershare';
		} elseif ($this->webType == 8) {
			$brand_info = 'iSkysoft';
		}
		
		foreach ($data as $value) {
			
			
			if (!empty($value->version_id) && ($value->status == 1)) {
				
				$sql = "SELECT  p.product_brand FROM won_product won_p 
				LEFT JOIN product p ON won_p.won_product_id = p.product_id 
				LEFT JOIN version v ON  p.product_id = v.product_id 
				WHERE  FIND_IN_SET(p.product_brand,'".$brand_info."') AND FIND_IN_SET(v.version_id,
				'".$value->version_id."') AND won_p.won_product_delete = 0 ";
				$nums = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql));
				if ($nums > 0) {
					
					$sql = "INSERT INTO online_help (id,version_id,title,sub_title,content,description,
					keywords,view_times,submit_ws_id,release_ws_id,created,released,status,hits,
					is_video,is_top,web_type) VALUES  
					('".$value->id."','".$value->version_id."','".addslashes($value->title)."','".addslashes($value->sub_title)."'
					,'".addslashes($value->content)."','".addslashes($value->description)."','".addslashes($value->keywords)."',
					'".$value->view_times."','".$value->submit_ws_id."','".$value->release_ws_id."','".$value->created."','".$value->released."',
					'".($value->status)."','".$value->hits."','".($value->is_vedio)."','".($value->is_top)."',".$this->webType.")";
					
					$GLOBALS['db']->query($sql);//清空分类表
				}
			}
		}

		if($_GET['syn_fage'] !=1)
		{
			echo  '<script type="text/javascript"> 
			alert("同步在线成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_online_help.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
}
$action    = $_REQUEST['action']; 
$faq_syn   = new FaqSyn;
if(method_exists($faq_syn, $action)) 
{
	$faq_syn->$action();
}else{
	$faq_syn->index();
}



?>